// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import VueRouter from 'vue-router'
import Vuex from 'vuex'
import { Message } from 'element-ui'
// 导入 ElementUI
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
//导入自己的router 规则
import router from './router'
import axios from 'axios'
import store from "./store"
import message from "./message/index.js"
//导入axios
Vue.prototype.axios = axios
Vue.prototype.message = message
Vue.use(Vuex)
//安装路由
Vue.use(VueRouter);
// 安装 ElementUI
Vue.use(ElementUI);

Vue.config.productionTip = false



axios.interceptors.request.use(config => {
  //给每个请求的添加一个baseUrl  并且带一个token
  config.baseURL = "http://192.168.6.176:2000";
  // config.baseURL="http://localhost:2000";
  //如果是登录  就直接跳走不带token 或者注册
  if (config.url.indexOf("login") != -1 || config.url.indexOf("register") != -1) {
    return config;
  }
  //这种方式在后台拿不到啊
  //  config.headers.token="token";

  //在本地缓存取token
  let token = localStorage.getItem("token");
  //取本次请求的参数
  let param = config.params;
  if (param) {   //为null 直接判断false
    //不为空  就加入token   
    param.token = token
  } else {
    //为空加  空指针 有错  就创建对象
    param = { "token": token }
  }
  config.params = param;
  return config;
}, error => {
  Message.error({ message: '请求超时!' });
  return Promise.resolve(error);
})



/*路由跳转前*/
router.beforeEach((to, from, next) => {
  let isLogin = localStorage.getItem("isLogin");
  //to  到哪儿
  if (to.path == "/logout") {
    localStorage.clear();
    next({ path: "/login" });

  } else if (to.path == "/login") {
    if (isLogin != null) {
      
      next({ path: "/main" })
    }
  } else if (isLogin == null) {
    next({ path: "/login" })
  }

  next();
});





//封装方法axios
//加上 asyns   await  类似于取消异步请求  等待方法执行完毕返回
Vue.prototype.$axiosGet = function (url, params) {
  let data = this.$axiso(url, params, "get");
  return data;
}

//postAxios
Vue.prototype.$axiosPost = async function (url, params) {
  let data = this.$axiso(url, params, "post");
  return data;

}


//封装axios    get  params  (后端 参数注解) requsetParam   post   data  (后端 参数注解) requestBody  => 
Vue.prototype.$axiso = async function (url, params, type) {
  //data  是用于区分get  post  传值    params  本来是get 使用params  post  使用data 
  // 但是一个解决不了  就都带着 
  let getData=params;
  let postData=params;
  if("get"==type){
   postData=null;
  }
  if("post"==type){
    getData=null;
  }

  let promise = new Promise((resolve, reject) => {
     
    this.axios({
      url: url,
      method: type,
      data: postData,
      params: getData
    }).then(result => {
      resolve(result);
    }).catch(err => {
      let status = err.request.status;
      console.log("status",status)
      if (status == 401) {
       message.warningMsg("您还未登陆 请先登录!");
       //token  没有  清除 
       localStorage.clear();
        this.$router.push({
          name: "Login",
        });
        return;
      }
     message.warningMsg("服务器繁忙!");
      reject(err);
    })
  })
  return promise;
}


/* eslint-disable no-new */
new Vue({
  el: '#app',
  store,
  router,
  // 启用 ElementUI c初始化
  render: h => h(App)
})
